define(function (require, exports, module) {

    require("apps/grid");
    require("apps/easyui");

    var ec = require("echarts/echarts");
    var theme = require("echarts/theme/macarons");

    function renderPie(data, dom){
        var option = {
            title : {
                text: '考勤统计',
                x: 'center'
            },
            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c}天 ({d}%)"
            },
            toolbox: {
                show : true,
                feature : {
//                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            legend: {
                orient : 'vertical',
                x : 'left',
                data: data.title
            },
            series : [
                {
                    name:'考勤事由',
                    type:'pie',
                    radius : '70%',
                    center: ['50%', '55%'],
                    selectedMode: 'single',
                    itemStyle : {
                        normal : {
                            label : {
                                position : 'outer',
                                formatter : function (params) {
                                    return params.name + "(" + params.value + "天)";
                                }
                            },
                            labelLine : {
                                show : true
                            }
                        }
                    },
                    data: data.content
                }
            ]
        };
        var myChart = ec.init(dom, theme);
        myChart.setOption(option);
        $("#globContent").on("resize", myChart.resize);
        $(window).on("resize", myChart.resize);
    }

    exports.init = function (ctx, uuid, config) {
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "attendance_tempate"
            },
            configs: {
                gridUrl: ctx + '/app/api/attendance/selfCountList',
                queryParams: $("[data-id=conditionForm]").serializeJson(),
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'reason.dirValue',
                        title: '考勤事由',
                        width: 90,
                        align: 'center'

                    },
                    {
                        field: 'remark',
                        title: '备注',
                        width: 160,
                        align: 'center'

                    },
                    {
                        field: 'applyUser.userName',
                        title: '人员',
                        width: 90,
                        align: 'center'
                    },
                    {
                        field: 'createUser.userName',
                        title: '填报人',
                        width: 100,
                        align: 'center',
                        formatter: function(value, row, index){
                            if(row.isReplace == 1){
                                return value + "(代填)"
                            }
                            return value;
                        }
                    },
                    {
                        field: 'createTime',
                        title: '填报时间',
                        width: 130,
                        align: 'center'
                    },
                    {
                        field: 'beginTime',
                        title: '开始时间',
                        width: 130,
                        align: 'center'
                    },
                    {
                        field: 'endTime',
                        title: '结束时间',
                        width: 130,
                        align: 'center',
                        formatter: function(value, row, index){
                            if(value == null){
                                return "暂无";
                            }
                            return value;
                        }
                    },
                    {
                        field: 'hourOrDay',
                        title: '小计',
                        width: 100,
                        align: 'center',
                        formatter: function(value, row, index){
                            if(value == null){
                                return "暂无";
                            }
                            return value;
                        }
                    }
                ]
            },
            events: {
                renderColBarBefore: function (type, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
//                            context.detail('考勤记录详情', row);
                            whenView(context, btn.href, row.id);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "export":
                            whenExport(btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var contextWindow = new $.PageWindow();

        var page = new $.PageDomFactory(uuid, true);

        function whenView(context, href, id){
            contextWindow.create({
                href: href + "?id=" + id,
                title: "考勤记录详情",
                width: 500
            });
            contextWindow.open();
        }

        function whenExport(href){
            var template = "序号=id=string;考勤事由=reason.dirValue=string;备注=remark=string;" +
                "所属小组=applyUser.orgsDescription=string;人员=applyUser.userName=string;" +
                "填报人=createUser.userName=string;填报时间=createTime=date[yyyy-MM-dd HH:mm];" +
                "开始时间=beginTime=date[yyyy-MM-dd HH:mm];结束时间=endTime=date[yyyy-MM-dd HH:mm];" +
                "小时数=hours=date[HH:mm];天数=days=string";
            var form = page.getJqDom("conditionForm");
            var action = form.attr("action");
            form.attr("action", href);
            page.getJqDom("template").attr("value", template);
            form.submit();
            form.attr("action", action);
            page.getJqDom("template").attr("value", "");
        }

        page.getJqDom("year").combobox({
            editable: false,
            onSelect: function(record){
                if(record.value == ''){
                    page.getJqDom("month").combobox('setValue', "");
                }
                dataCount();
            }
        });
        page.getJqDom("month").combobox({
            editable: false,
            onSelect: function(){
                dataCount();
            }
        });
        page.getJqDom("reason").combobox({
            editable: false,
            onSelect: function(){
                var postData = page.getJqDom("conditionForm").serializeJson();
                pageListComponent.grid.datagrid("load", postData);
            }
        });

        var pieDom = document.getElementById("echartPie");
        function dataCount(){
            var postForm = page.getJqDom("conditionForm");
            var postData = postForm.serializeJson();
            $("body").wait("start", "查询中,请稍候.");
            $.post(postForm.attr("action") + "?random=" + Math.random(),
                $.utils.buildJson(postData),
                function(json){
                    $("body").wait("end");
                    renderPie(json, pieDom);
                }, "json");
            pageListComponent.grid.datagrid("load", postData);
        }

        function dataCountInit(){
            var postForm = page.getJqDom("conditionForm");
            var postData = postForm.serializeJson();
            $("body").wait("start", "查询中,请稍候.");
            $.post(postForm.attr("action") + "?random=" + Math.random(),
                $.utils.buildJson(postData),
                function(json){
                    $("body").wait("end");
                    renderPie(json, pieDom);
                }, "json");
        }
        dataCountInit();

    };

});